home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 2 / Apprentice-Release2.iso / Tools / Languages / Caml Light 0.61 / Source / src / runtime / memory.h < prev    next >
Encoding:
C/C++ Source or Header  |  1993-09-24  |  3.0 KB  |  97 lines  |  [TEXT/MPS ]

  1. /* Allocation macros and functions */
  2.  
  3. #ifndef _memory_
  4. #define _memory_
  5.  
  6.  
  7. #include "config.h"
  8. #include "gc.h"
  9. #include "major_gc.h"
  10. #include "minor_gc.h"
  11. #include "misc.h"
  12. #include "mlvalues.h"
  13.  
  14. extern value *c_roots_head;
  15.  
  16. #ifdef ANSI
  17.  
  18. extern void init_memory (asize_t, asize_t);
  19. extern value raw_alloc_shr (mlsize_t, tag_t);
  20. extern value alloc_shr (mlsize_t, tag_t);
  21. extern void modify (value *, value);
  22. extern void initialize (value *, value);
  23. extern char * stat_alloc (asize_t);            /* Size in bytes. */
  24. extern void stat_free (char *);
  25. extern char * stat_resize (char *, asize_t);     /* Size in bytes. */
  26.  
  27. #else
  28.  
  29. void init_memory ();
  30. value raw_alloc_shr ();
  31. value alloc_shr ();
  32. void modify ();
  33. void initialize ();
  34. char * stat_alloc ();        /* Size in bytes. */
  35. void stat_free ();
  36. char * stat_resize ();        /* Size in bytes. */
  37.  
  38. #endif /* ANSI */
  39.  
  40. #define Alloc_small(result, wosize, tag) {                      \
  41.   char *_res_ = young_ptr;                              \
  42.   young_ptr += Bhsize_wosize (wosize);                          \
  43.   if (young_ptr > young_end){                              \
  44.     Setup_for_gc;                                  \
  45.     minor_collection ();                              \
  46.     Restore_after_gc;                                  \
  47.     _res_ = young_ptr;                                  \
  48.     young_ptr += Bhsize_wosize (wosize);                      \
  49.   }                                          \
  50.   Hd_hp (_res_) = Make_header ((wosize), (tag), Black);                  \
  51.   (result) = Val_hp (_res_);                              \
  52. }
  53.  
  54. /* You must use [Modify] to change a field of an existing shared block,
  55.    unless you are sure the value being overwritten is not a shared block and
  56.    the value being written is not a young block. */
  57. /* [Modify] never calls the GC. */
  58. #define Modify(fp, val) {                              \
  59.   value _old_ = *(fp);                                  \
  60.   *(fp) = (val);                                  \
  61.   if (Is_in_heap (fp)){                                  \
  62.     if (gc_phase == Phase_mark) darken (_old_);                      \
  63.     if (Is_block (val) && Is_young (val)                      \
  64.     && ! (Is_block (_old_) && Is_young (_old_))){                  \
  65.       *ref_table_ptr++ = (fp);                              \
  66.       if (ref_table_ptr >= ref_table_end){                      \
  67.         Assert (ref_table_ptr == ref_table_end);                  \
  68.     realloc_ref_table ();                              \
  69.       }                                          \
  70.     }                                          \
  71.   }                                          \
  72. }
  73.  
  74. /* [Push_roots] and [Pop_roots] are used for C variables that are GC roots.
  75.  * It must contain all values in C local variables at the time the minor GC is
  76.  * called.
  77.  * Usage:
  78.  * At the end of the declarations of your C local variables, add
  79.  * [ Push_roots (variable_name, size); ]
  80.  * The size is the number of declared roots.  They are accessed as
  81.  * [ variable_name [0] ... variable_name [size - 1] ].
  82.  * The [variable_name] and the [size] must not be [ _ ].
  83.  * Just before the function return, add a call to [Pop_roots].
  84.  */
  85.  
  86. #define Push_roots(name, size)                              \
  87.    value name [(size) + 2];                              \
  88.    { long _; for (_ = 0; _ < (size); name [_++] = Val_long (0)); }          \
  89.    name [(size)] = (value) (size);                          \
  90.    name [(size) + 1] = (value) c_roots_head;                      \
  91.    c_roots_head = &(name [(size)]);
  92.  
  93. #define Pop_roots() {c_roots_head = (value *) c_roots_head [1]; }
  94.  
  95.  
  96. #endif /* _memory_ */
  97.